<?php
// +---------------------------------------------------------------------------------
// | ECODE [ 随心而行 勿忘始终]
// +---------------------------------------------------------------------------------
// | Copyright (c) 2017 ECODE All rights reserved.
// +---------------------------------------------------------------------------------
// | Author: 鬼谷子 <majianwei168@outlook.com>
// +---------------------------------------------------------------------------------
namespace app\admin\model;
use think\Model;

class Category extends Model
{

    protected $autoWriteTimestamp = true;
    protected $type     = ['create_time'  =>  'timestamp', 'update_time' => 'timestamp'];

    protected function initialize()
    {
        parent::initialize();
    }

    public function lists($map='', $order='ID DESC', $limit=20)
    {
        $obj = $this->where($map)->order($order)->paginate($limit);
        $list= $obj->toArray();
        $page= $obj->render();
        $data['list'] = $list['data'];
        $data['page'] = $page;
        return $data;
    }

    public function updates($data, $id=null)
    {
        $where = $id ? array('id' => $id) : null;
        $res = $this->validate(true)->save($data, $where);
        if( $res === false) {
            return $this->getError();
        } else {
            return $id ? $res : (int)$this->id;
        }
    }

    public function getone($id)
    {
    	$obj = $this->where(array('id' => $id))->find();
    	return $obj->getData();
    }

    public function del($id)
    {
        $data = db('Category')->where(array('pid' => $id))->find();
        if($data == NULL) {
            $this::destroy($id);
            return 1;
        } else {
            return -1;
        }

    }

    protected function getStatusAttr($value)
    {
        $status = [1=>'显示',-1=>'隐藏'];
        return $status[$value];
    }

    public function getMessage($code) 
    {

    	switch ($code) {
    		case -1:
    			$message = '标题不能为空';
    			break;
    		case -2:
    			$message = '标识不能为空';
    			break;
    		default:
    			$message = '未知错误';
    			break;
    	}
    	return $message;
    }

}
